package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCar(ShoppingCartDTO shoppingCartDTO) {
        //查询菜品
        Dish dish = dishMapper.findDish(shoppingCartDTO);
        //查询套餐
        Setmeal setmeal = setmealMapper.findSetmeal(shoppingCartDTO);
        //获取用户ID
        Long userId = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //判断是不是菜品,如果是就赋值
        if (dish != null){
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }
        //否则是套餐
        else {
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setUserId(userId);

        //查询是否有这一个订单
        ShoppingCart num = shoppingCartMapper.findNum(userId, shoppingCartDTO);
        //如果没用该购物车,第一次添加数量默认1
        if (num == null){
            shoppingCart.setNumber(1);
            //如果有该购物车,则数量加1
        }else {
             shoppingCart.setNumber(num.getNumber()+1);
        }
        shoppingCart.setCreateTime(LocalDateTime.now());
        //没有购物车则添加
        if (num == null){
            shoppingCartMapper.addShoppingCar(shoppingCart);
            //有该购物车则更数量
        }else {
            shoppingCartMapper.upNumberaddShoppingCar(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> ViewShoppingCar() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.ViewShoppingCar(userId);
        return shoppingCarts;
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void UsbShoppingCar(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        //购物车数量-1
        shoppingCartMapper.UsbShoppingCar(shoppingCartDTO,userId);
        //删除购物车中一个商品
        shoppingCartMapper.delNum0ShoppingCar(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void DeleteShoppingCar() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.DeleteShoppingCar(userId);
    }
}
